package com.itheima.consumer.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Slf4j
@Component
public class SpringRabbitListener {
    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容

    //消息不会丢失
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(Message msg){
        System.out.println("spring 消费者接收到消息 ID：【" + msg.getMessageProperties().getMessageId() + "】");
        System.out.println("spring 消费者接收到消息 ：【" + new String(msg.getBody()) + "】");
//        throw new RuntimeException("业务异常");
    }
    //消息会丢失
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage2(String msg){
//        System.out.println("spring 消费者接收到消息：【" + msg + "】");
//        throw new MessageConversionException("消息转换异常异常");
//    }
    //定义两个消息接收者
//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue1(String msg) throws InterruptedException {
//        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(20);
//    }
//
//    @RabbitListener(queues = "work.queue")
//    public void listenWorkQueue2(String msg) throws InterruptedException {
//        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
//        Thread.sleep(200);
//    }
//    @RabbitListener(queues = "fanout.queue1")
//    public void listenFanoutQueue1(String msg) {
//        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "fanout.queue2")
//    public void listenFanoutQueue2(String msg) {
//        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
//    }

//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue1",durable = "true")
//            ,exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
//            key = {"red","blue"}
//    ))
//    public void listenDirectQueue1(String msg) {
//        System.out.println("消费者1接收到direct.queue1的消息：【" + msg + "】");
//    }
//
//    @RabbitListener(bindings = @QueueBinding(
//            value = @Queue(name = "direct.queue2",durable = "true")
//            ,exchange = @Exchange(name = "hmall.direct",type = ExchangeTypes.DIRECT),
//            key = {"red","yellow"}
//    ))
//    public void listenDirectQueue2(String msg) {
//        System.out.println("消费者2接收到direct.queue2的消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "topic.queue1")
//    public void listenTopicQueue1(String msg){
//        System.out.println("消费者1接收到topic.queue1的消息：【" + msg + "】");
//    }
//
//    @RabbitListener(queues = "topic.queue2")
//    public void listenTopicQueue2(String msg){
//        System.out.println("消费者2接收到topic.queue2的消息：【" + msg + "】");
//    }
//    @RabbitListener(queues = "object.queue")
//    public void listenSimpleQueueMessage(Map<String, Object> msg) throws InterruptedException {
//        System.out.println("消费者接收到object.queue消息：【" + msg + "】");
//    }
        @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "dlx.queue",durable = "true")
            ,exchange = @Exchange(name = "dlx.direct",type = ExchangeTypes.DIRECT),
            key = {"hi"}
    ))
    public void listenDlxQueue(String msg) {
        System.out.println("消费者1接收到dlx.queue的消息：【" + msg + "】");
    }
    //创建延迟队列和转换机，并绑定队列到转换机，并指定路由键
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "delay"
    ))
    public void listenDelayMessage(String msg){
        log.info("接收到delay.queue的延迟消息：{}", msg);
    }
}
